const config = require('./config');

class Database {
  constructor() {
    this.connection = null;
    this.isConnected = false;
  }

  // 连接数据库（示例为MongoDB，可根据需要修改）
  async connect() {
    try {
      // 这里可以根据实际使用的数据库进行配置
      // 例如：MongoDB, MySQL, PostgreSQL等
      
      if (config.database.url) {
        // 使用连接字符串
        console.log('Connecting to database using connection string...');
        // this.connection = await mongoose.connect(config.database.url);
      } else {
        // 使用单独配置
        const connectionString = `mongodb://${config.database.host}:${config.database.port}/${config.database.name}`;
        console.log(`Connecting to database: ${connectionString}`);
        // this.connection = await mongoose.connect(connectionString);
      }

      this.isConnected = true;
      console.log('Database connected successfully');
    } catch (error) {
      console.error('Database connection failed:', error);
      this.isConnected = false;
      throw error;
    }
  }

  // 断开数据库连接
  async disconnect() {
    try {
      if (this.connection) {
        // await this.connection.disconnect();
        this.connection = null;
        this.isConnected = false;
        console.log('Database disconnected successfully');
      }
    } catch (error) {
      console.error('Database disconnection failed:', error);
      throw error;
    }
  }

  // 检查连接状态
  getConnectionStatus() {
    return {
      isConnected: this.isConnected,
      connection: this.connection
    };
  }
}

module.exports = new Database();
